import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    sendmessagelist: [],
    messagelist: [],
  },
  mutations: {
    creatlist(state,msg){
      state.messagelist = [...msg]
      state.messagelist.map(r=>{
        r.tofrom = JSON.parse(r.tofrom)
        r.touser = JSON.parse(r.touser)
      })
      const unmsg = msg.sort((a,b)=>{
        return b.imid - a.imid;
      })
      const uid = localStorage.getItem('uid');
      console.info('是不是倒叙',unmsg)
      unmsg.forEach(r=>{
        if(r.to == uid){
          if(!state.sendmessagelist.some(item => item.from == r.from)){
            state.sendmessagelist.push(r)
          }
        }
      })
      console.info('消息列表',state.sendmessagelist)
    },
    messlist(state,msg){
      console.log('msgis',msg)
      console.info('列表',state.messagelist)
      state.messagelist.push(msg)
    },
    setlist(state,list){
      state.sendmessagelist.push(list)
      console.info('第一次',list)
    },
    replace(state,list){
      for(let i = 0; i< state.sendmessagelist.length; i++){
        if(state.sendmessagelist[i].from == list.from){
          state.sendmessagelist.splice(i,1,list)
        }
      }

    },
    del(state,list){
      state.sendmessagelist.push(list)
    },
    getuserinfo(state,data){
      console.info(data)
    }
  },
  actions: {

  },
  modules: {
  }
});
